<template>
  <div class="app-container">
    <!-- 高级查询 -->
    <el-form v-show="searchVisible" ref="queryForm" :model="queryParams" :inline="true">

      <el-form-item label="收货人名称:">
        <el-input v-model="queryParams.consigneeName" size="small" placeholder="请输入收货人名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>



      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button  type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button  type="success" icon="el-icon-edit" size="mini" :disabled="isSingle" @click="handleEdit">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button  type="danger" icon="el-icon-delete" size="mini" :disabled="isMultiple" @click="handleRemove">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button  type="warning" icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
      </el-col>
      <right-toolbar :show-search.sync="searchVisible" @queryTable="handleQuery" />
    </el-row>

    <!-- 数据列表 -->
    <el-table v-loading="loading" :data="page.records" highlight-current-row @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="客户编码" align="center" prop="customerNo" />
      <el-table-column label="收货人名称" align="center" prop="consigneeName" >
        <template slot-scope="scope">
          <el-button type="text" @click="handleView(scope.row)">{{ scope.row.consigneeName }}</el-button>
        </template>
      </el-table-column>

      <el-table-column label="所在城市" align="center" prop="areaDesc" />
      <el-table-column label="地址" align="center" prop="consigneeCompanyAddress" />
      <el-table-column label="发票地址" align="center" prop="invoiceAddress" />
      <el-table-column label="收货地址" align="center" prop="consigneeAddress" />
      <el-table-column label="联系人" align="center" prop="linkMan" />
      <el-table-column label="手机号" align="center" prop="linkManMobile" />



      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="创建时间" align="center" prop="createDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updateDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-hasPermission="['basic:customerConsignee:update']" size="mini" type="text" icon="el-icon-edit" @click="handleEdit(scope.row)">修改</el-button>
          <el-button v-hasPermission="['basic:customerConsignee:delete']" size="mini" type="text" icon="el-icon-delete" @click="handleRemove(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination v-show="page.total>0" :total="page.total" :page.sync="page.current" :limit.sync="page.pageSize" @pagination="handleQuery" />

    <!-- 添加/修改对话框 -->
    <el-dialog :title="customerConsigneeFormDialog.title" :visible.sync="customerConsigneeFormDialog.visible" :close-on-click-modal="false" :before-close="resetCustomerConsigneeFormDialog" width="700px" append-to-body>
      <el-form ref="customerConsigneeForm" :model="customerConsigneeFormData" :rules="customerConsigneeFormRules" label-width="100px">
        <el-form-item label="客户编码" prop="customerNo">
          <el-select v-model="customerConsigneeFormData.customerNo" filterable clearable placeholder="请选择">
          	<el-option v-for="item in  options.customers" :key="item.id" :label="item.customerName"
          		:value="item.id">
          	</el-option>

          </el-select>

        </el-form-item>

        <el-form-item label="收货人名称" prop="consigneeName">
          <el-input v-model="customerConsigneeFormData.consigneeName" placeholder="请输入收货人名称" />
        </el-form-item>

     <!--   <el-form-item label="所在城市" prop="areaDesc"> -->
          <el-form-item label="所在城市：" prop="departureIdList">
             <el-cascader
               v-model="customerConsigneeFormData.departureIdList"
               :options="options.locationList"
               :props="{ expandTrigger: 'hover', label: 'locationName', value: 'id' }"

             />
           </el-form-item>
      <!--    <el-input v-model="customerConsigneeFormData.areaDesc" placeholder="请输入所在城市" />
        </el-form-item> -->
        <el-form-item label="地址" prop="consigneeCompanyAddress">
          <el-input v-model="customerConsigneeFormData.consigneeCompanyAddress" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="发票地址" prop="invoiceAddress">
          <el-input v-model="customerConsigneeFormData.invoiceAddress" placeholder="请输入发票地址" />
        </el-form-item>
        <el-form-item label="收货地址" prop="consigneeAddress">
          <el-input v-model="customerConsigneeFormData.consigneeAddress" placeholder="请输入收货地址" />
        </el-form-item>
        <el-form-item label="联系人" prop="linkMan">
          <el-input v-model="customerConsigneeFormData.linkMan" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="手机号" prop="linkManMobile">
          <el-input v-model="customerConsigneeFormData.linkManMobile" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="仓库" prop="warehouseCode">
          <el-select v-model="customerConsigneeFormData.warehouseCode" filterable clearable placeholder="请选择"
          @change="change">
          	<el-option v-for="item in  options.warehouseList" :key="item.id" :label="item.warehouserName"
          		:value="item.id">
          	</el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="库区" prop="warehouseArea">
          <el-select v-model="customerConsigneeFormData.warehouseArea" filterable clearable placeholder="请选择">
          	<el-option v-for="item in  options.warehouseAreaList" :key="item.id" :label="item.areaName"
          		:value="item.id">
          	</el-option>
          </el-select>
        </el-form-item>


        <el-form-item label="备注" prop="remark">
          <el-input v-model="customerConsigneeFormData.remark" placeholder="请输入备注" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confirmCustomerConsigneeFormDialog">确 定</el-button>
        <el-button @click="resetCustomerConsigneeFormDialog">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import CustomerConsigneeApi from '@/api/basic/CustomerConsigneeApi'
import CustomerApi from '@/api/basic/CustomerApi'
import TreeUtils from '@/utils/TreeUtils'
import LocationApi from '@/api/basic/LocationApi'
import WarehouseApi from '@/api/basic/WarehouseApi'
import WarehouseAreaApi from '@/api/web/WarehouseAreaApi'
const defaultCustomerConsigneeFormData = {
  customerNo: null,  customerRefNo: null,  consigneeName: null,  consigneeShortName: null,  areaDesc: null,
   consigneeCompanyAddress: null,  invoiceAddress: null,  consigneeAddress: null,  linkMan: null,  linkManMobile: null,
   linkManPhone: null,  linkManFax: null,  noteText: null,  userId: null,  version: null,  textMsgYn: null,  email: null,
     isInternationalYn: null,  halfTaget: null,  qaterTaget: null,  buyHowmanyGetOneFree: null,  giftPercentage: null,
      freeGiftPctPershipment: null,  unitPriceCoef: null,  consigneeType: null,  companyNo: null,  sellsmanager: null,
       site: null,  ccuscode: null,  ccusregcode: null,  ccusbank: null,  ccusaccount: null,  ccusheadcode: null,  dcusdevdate: null,
        cmodifyperson: null,  ccreateperson: null,  dmodifydate: null,  pubufts: null,  cinvoicecompany: null,  bcusstate: null, departureIdList : null,
         bcusdomestic: null,  ccusexchName: null,  icostgrade: null,  ccuscreditcompany: null,  ccccode: null,  about: null,  createDate: null,  updateDate: null,warehouseCode: null,warehouseArea: null}
export default {
  name: 'CustomerConsigneeList',
  data() {
    return {
      options: {
        sysYnTypeOptions: this.$constant.sysYnTypeOptions,
        locationList: [],
        customers: [],
        warehouseList: [],
        warehouseAreaList: []
      },
      queryParams: {
        customerNo: null,
        customerRefNo: null,
        consigneeName: null,
        consigneeShortName: null,
        areaDesc: null,
        consigneeCompanyAddress: null,
        invoiceAddress: null,
        consigneeAddress: null,
        linkMan: null,
        linkManMobile: null,
        linkManPhone: null,
        linkManFax: null,
        noteText: null,
        userId: null,
        version: null,
        textMsgYn: null,
        email: null,
        isInternationalYn: null,
        halfTaget: null,
        qaterTaget: null,
        buyHowmanyGetOneFree: null,
        giftPercentage: null,
        freeGiftPctPershipment: null,
        unitPriceCoef: null,
        consigneeType: null,
        companyNo: null,
        sellsmanager: null,
        site: null,
        ccuscode: null,
        ccusregcode: null,
        ccusbank: null,
        ccusaccount: null,
        ccusheadcode: null,
        dcusdevdate: null,
        cmodifyperson: null,
        ccreateperson: null,
        dmodifydate: null,
        pubufts: null,
        cinvoicecompany: null,
        bcusstate: null,
        bcusdomestic: null,
        ccusexchName: null,
        icostgrade: null,
        ccuscreditcompany: null,
        ccccode: null,
        about: null,
        createDate: null,
        updateDate: null,
        isValid: this.$constant.Y
      },
      page: {
        current: 1,
        pageSize: 10,
        total: 0,
        records: []
      },
      customerConsigneeFormDialog: {
        title: '',
        visible: false
      },
      customerConsigneeFormData: Object.assign({}, defaultCustomerConsigneeFormData),
      customerConsigneeFormRules: {
        createDate: [{ required: true, message: '创建时间不能为空', trigger: 'blur' }],
        updateDate: [{ required: true, message: '更新时间不能为空', trigger: 'blur' }]
      },
      loading: true,
      searchVisible: true,
      multipleSelection: [],
      isSingle: true,
      isMultiple: true
    }
  },
  created() {
    this.handleQuery()
    this.loadOptions()
  },
  methods: {
    loadOptions() {
      LocationApi.list().then(response => {
         this.options.locationList = TreeUtils.list2Tree(response.data)
       })

       CustomerApi.list().then(res => {
       	this.options.customers = res.data

       })

       WarehouseApi.list({warehouseType:'center'}).then(res =>{
         this.options.warehouseList = res.data
       })
    },
    handleAdd() {
      this.openCustomerConsigneeFormDialog()
    },
    handleRemove(row) {
      const ids = row.id || this.multipleSelection.map(item => item.id)
      this.$confirm('是否确认删除数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return CustomerConsigneeApi.remove(ids)
      }).then(() => {
        this.handleQuery()
        this.$message.success('删除成功')
      })
    },
    handleEdit(row) {
      const id = row.id || this.multipleSelection.map(item => item.id)[0]
      this.openCustomerConsigneeFormDialog(id)
    },
    handleQuery() {
      this.loading = true
      CustomerConsigneeApi.page(this.queryParams, this.page).then(response => {
        this.page.records = response.data.records
        this.page.total = response.data.total
        this.loading = false
      }, () => {
        this.loading = false
      })
    },
    handleExport() {
      this.$confirm('是否确认导出数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return CustomerConsigneeApi.exportData(this.queryParams)
      }).then(response => {
        this.$common.download(response.msg)
      })
    },
    openCustomerConsigneeFormDialog(id) {
      this.customerConsigneeFormDialog.visible = true
      if (id) {
        this.customerConsigneeFormDialog.title = '客户收件人-修改'
        CustomerConsigneeApi.get(id).then(response => {
          this.customerConsigneeFormData = {
            ...this.customerConsigneeFormData,
            ...response.data
          }
        })
      } else {
        this.customerConsigneeFormDialog.title = '客户收件人-添加'
      }
    },
    confirmCustomerConsigneeFormDialog() {
      this.$refs['customerConsigneeForm'].validate(valid => {
        if (valid) {
          if (this.customerConsigneeFormData.id) {
            if (this.customerConsigneeFormData.departureIdList) {
                   this.customerConsigneeFormData.areaDesc= this.customerConsigneeFormData.departureIdList.join(',')
                 }
            CustomerConsigneeApi.edit(this.customerConsigneeFormData).then(response => {
              this.$message.success('修改成功')
              this.resetCustomerConsigneeFormDialog()
              this.handleQuery()
            })
          } else {
            if (this.customerConsigneeFormData.departureIdList) {
                   this.customerConsigneeFormData.areaDesc= this.customerConsigneeFormData.departureIdList.join(',')
                 }
            CustomerConsigneeApi.add(this.customerConsigneeFormData).then(response => {
              this.$message.success('添加成功')
              this.resetCustomerConsigneeFormDialog()
              this.handleQuery()
            })
          }
        }
      })
    },
    resetCustomerConsigneeFormDialog() {
      this.customerConsigneeFormDialog = {
        title: '',
        visible: false
      }
      this.customerConsigneeFormData = Object.assign({}, defaultCustomerConsigneeFormData)
      this.resetForm('customerConsigneeForm')
    },
    handleSelectionChange(selection) {
      this.multipleSelection = selection
      this.isSingle = selection.length !== 1
      this.isMultiple = !selection.length
    },
    change(v){

      WarehouseAreaApi.list({warehouseId:v}).then(res =>{
        this.options.warehouseAreaList = res.data
      })
    },
    handleView(row) {
      this.$router.push({ path: '/customerConsignee/view/' + row.id })
    },
    resetQuery() {
      this.page.current = 1
      this.resetForm('queryForm')
      this.handleQuery()
    }
  }
}
</script>
